/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeMap = argot.TypeMap;
using TypeOutputStream = argot.TypeOutputStream;
using TypeLibrary = argot.TypeLibrary;
using BigEndianSignedInteger = argot.common.BigEndianSignedInteger;
using BigEndianUnsignedByte = argot.common.BigEndianUnsignedByte;
using U8Boolean = argot.common.U8Boolean;


namespace argot.network
{
	
	public class TypeClient
	{
		private TypeLink _link;
		private ProtocolTypeMap _typeMap;
		
		public TypeClient(TypeLibrary library, TypeLink link)
		{
			_link = link;
			_typeMap = new ProtocolTypeMap(library);
		}
		
		public virtual int ResolveType(System.String name, byte[] definition)
		{
			// Write the name and definition to the request body.	
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream tmos = new TypeOutputStream(baos, _typeMap);
			tmos.WriteObject("u8", (System.Object) ProtocolTypeMap.MAP);
			tmos.WriteObject("u8ascii", name);
			tmos.WriteObject("u16binary", definition);
			baos.Close();
			
			// Send the request to the server.
			byte[] response = _link.ProcessMessage(baos.ToArray());
			
			System.IO.MemoryStream bais = new System.IO.MemoryStream(response);
			TypeInputStream tmis = new TypeInputStream(bais, _typeMap);
			System.Byte type = (System.Byte) tmis.ReadObject(BigEndianUnsignedByte.TYPENAME);
			if ( type != ProtocolTypeMap.MAP )
				throw new TypeException("Bad Protocol Error");
			System.Int32 value_Renamed = (System.Int32) tmis.ReadObject(BigEndianSignedInteger.TYPENAME);
			bais.Close();
			
			return value_Renamed;
		}
		
		public virtual int ReserveType(System.String name)
		{
			// Write the name and definition to the request body.	
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream tmos = new TypeOutputStream(baos, _typeMap);
			tmos.WriteObject("u8", (System.Object) ProtocolTypeMap.MAPRES);
			tmos.WriteObject("u8ascii", name);
			baos.Close();
			
			// Send the request to the server.
			byte[] response = _link.ProcessMessage(baos.ToArray());
			
			System.IO.MemoryStream bais = new System.IO.MemoryStream(response);
			TypeInputStream tmis = new TypeInputStream(bais, _typeMap);
			System.Byte type = (System.Byte) tmis.ReadObject(BigEndianUnsignedByte.TYPENAME);
			if (type != ProtocolTypeMap.MAPRES)
				throw new TypeException("Bad Protocol Error");
			System.Int32 value_Renamed = (System.Int32) tmis.ReadObject(BigEndianSignedInteger.TYPENAME);
			bais.Close();
			
			return value_Renamed;
		}
		
		public virtual TypeTriple ResolveReverse(int id)
		{
			// Write the name and definition to the request body.
			System.IO.MemoryStream baos = new System.IO.MemoryStream();
			TypeOutputStream tmos = new TypeOutputStream(baos, _typeMap);
			tmos.WriteObject("u8", (System.Object) ProtocolTypeMap.MAPREV);
			tmos.WriteObject(BigEndianSignedInteger.TYPENAME, (System.Object) id);
			baos.Close();
			
			// Send the request to the server.
			byte[] response = _link.ProcessMessage(baos.ToArray());
			
			System.IO.MemoryStream bais = new System.IO.MemoryStream(response);
			TypeInputStream tmis = new TypeInputStream(bais, _typeMap);
			System.Byte type = (System.Byte) tmis.ReadObject(BigEndianUnsignedByte.TYPENAME);
			if ( type != ProtocolTypeMap.MAPREV )
				throw new TypeException("Bad Protocol Error");
			System.String name = (System.String) tmis.ReadObject("u8ascii");
			byte[] definition = (byte[]) tmis.ReadObject("u16binary");
			bais.Close();
			
			return new TypeTriple(id, name, definition);
		}

	}
}